import { BaseDeployPanel } from "./BaseDeployPanel";
import { Language } from "../../../../../../extend/language/Language";
import { ObjCreate } from "../../../common/ObjCreate";
import { GTextFormat } from "../../../../../common/GTextFormat";
import { GImageBitmap } from "../../../../../../com/mui/controls/GImageBitmap";
import { DeployPanelItem } from "../DeployPanelItem";
import { NumInput } from "../../../common/NumInput";
import { FilterConst } from "../../../../../component/gconst/FilterConst";
import { Alert } from "../../../../../../com/mui/controls/Alert";
import { MsgManager } from "../../../../manager/MsgManager";
import { Cache } from "../../../../cache/Cache";
import { DataEvent } from "../../../../events/DataEvent";
import { EquipmentModuleConst } from "../../data/EquipmentModuleConst";
import { NetDispatcher } from "../../../../../mvc/core/NetDispatcher";
import { ServerCommand } from "../../../../mvc/ServerCommand";
import { EquipUpdateParams } from "../../data/EquipUpdateParams";
import { ItemData } from "../../../../resource/info/ItemData";
import { EquipAdvanceConfig } from "../../../../resource/EquipAdvanceConfig";
import { ECategory } from "../../../../../../Message/Public/ECategory";
import { ItemConfig } from "../../../../resource/ItemConfig";
import { EProp } from "../../../../../../Message/Public/EProp";
import { EBind } from "../../../../../../Message/Public/EBind";
import { ItemsUtil } from "../../../shortcuts/item/ItemsUtil";
import { EPlayerItemPosType } from "../../../../../../Message/Public/EPlayerItemPosType";
import { EItemUpdateResult } from "../../../../../../Message/Game/EItemUpdateResult";
type int = number;
//class JewelAdvancePanel
    
    export  class JewelAdvancePanel extends BaseDeployPanel
    {
        constructor()
        {
            this._materialsOwn = [];
            this._useItems = [];
            this.posData = [{"x": 100, "y": 115, "label": Language.getString(61064), "canRemove": true, "noBoarder": true}, {"x": 208, "y": 115, "label": Language.getString(61065), "canRemove": false, "noBoarder": true}, {"x": 156, "y": 245, "label": Language.getString(61066), "canRemove": false, "noBoarder": false}];
            this._alarmText = Language.getString(60147);
            this.buttonLabel = Language.getString(61063);
            super();
            return;
        }

        protected /* override */ createChildren(): void
        {
            var loc1=null;
            var loc4=null;
            var loc5=null;
            super.createChildren();
            loc1 = ObjCreate.createLabel(Language.getString(61062), 0, 66, 350, 25, flash.text.TextFormatAlign.CENTER, this, true, 16445696);
            loc1.setStyle("textFormat", new GTextFormat("LiSu", 20, 16776960, null, null, null, null, null, flash.text.TextFormatAlign.CENTER));
            var loc2=new GImageBitmap("EquipMerge_arrow.swf", 118, 127);
            this.addChild(loc2);
            var loc3=0;
            while (loc3 < this.posData.length) 
            {
                loc4 = this.posData[loc3];
                (loc5 = new DeployPanelItem(loc3, loc4.noBoarder)).posIndex = loc3;
                loc5.x = loc4["x"];
                loc5.y = loc4["y"];
                loc5.label = loc4["label"];
                if (loc4.canRemove) 
                {
                    loc5.mouseEnabled = true;
                    loc5.addEventListener(flash.events.MouseEvent.CLICK, this.onItemClick);
                }
                addChild(loc5);
                allItem.push(loc5);
                ++loc3;
            }
            this._numInput = new NumInput();
            this._numInput.x = 220;
            this._numInput.y = 310;
            this._numInput.width = 87;
            this._numInput.height = 20;
            this.addChild(this._numInput);
            this._numInput.addEventListener(flash.events.Event.CHANGE, this.onNumChange);
            confirmButton.label = this.buttonLabel;
            confirmButton.x = 140;
            confirmButton.y = 305;
            confirmButton.textField.filters = [FilterConst.glowFilter];
            confirmButton.styleName = "ButtonSpecial";
            addChild(confirmButton);
            confirmButton.addEventListener(flash.events.MouseEvent.CLICK, this.onButtonClick);
            return;
        }

        /* internal  */onItemClick(arg1: Object /* flash.events.MouseEvent */): void
        {
            var loc1=arg1.currentTarget as DeployPanelItem;
            this.moveOutByIndex(loc1.posIndex);
            return;
        }

        /* internal  */onNumChange(arg1: Object /* flash.events.Event */): void
        {
            if (allItem[0].itemData) 
            {
                allItem[0].amount = this._numInput.currentNum;
                this.updateMaterial();
            }
            return;
        }

        /* internal  */onButtonClick(arg1: Object /* flash.events.MouseEvent */): void
        {
            this.onButtonClickImpl(arg1.target);
            return;
        }

        /* internal  */onButtonClickImpl(arg1): void
        {
            var target;

            var loc1;
            target = arg1;
            if (!this.checkCanAdvance()) 
            {
                return;
            }
            var loc2=target;
            switch (loc2) 
            {
                case confirmButton:
                {
                    if (this.checkWillChangeBind()) 
                    {
                        Alert.show(this._alarmText, Language.getString(69900), Alert.OK | Alert.CANCEL, null, (arg1: int): void
                        {
                            if (arg1 == Alert.OK) 
                            {
                                onConfirm();
                            }
                            return;
                        })
                    }
                    else 
                    {
                        this.onConfirm();
                    }
                    break;
                }
            }
            return;
        }

        /* internal  */checkCanAdvance(): Boolean
        {
            if (!allItem[0].itemData) 
            {
                MsgManager.showRollTipsMsg(Language.getString(61062));
                return false;
            }
            if (!allItem[1].itemData || allItem[1].enabled == false) 
            {
                if (Cache.instance.equipment.isAutoBuy && allItem[1].itemData) 
                {
                    return true;
                }
                MsgManager.showRollTipsMsg(Language.getString(61067));
                return false;
            }
            if (!isEnoughMoney) 
            {
                this.dispatchEvent(new DataEvent(EquipmentModuleConst.Event_CoinNotEnough, null, false, true, true));
                MsgManager.showRollTipsMsg(Language.getString(60121));
                return false;
            }
            return true;
        }

        /* internal  */getLackAmount(): int
        {
            var loc2=null;
            var loc3=0;
            var loc1=0;
            var loc4=0;
            var loc5=this._useItems;
            for(loc2 of loc5) 
            {
                loc1 = loc1 + loc2.itemAmount;
            }
            loc3 = this._numInput.currentNum - loc1;
            return loc3 < 0 ? 0 : loc3;
        }

        /* internal  */onItemBuySuccess(): void
        {
            NetDispatcher.addCmdListener(ServerCommand.BackPackItemsChange, this.serverBagDataChangehandler);
            return;
        }

        /* internal  */serverBagDataChangehandler(arg1): void
        {
            NetDispatcher.removeCmdListener(ServerCommand.BackPackItemsChange, this.serverBagDataChangehandler);
            if (allItem[0].itemData) 
            {
                this.dispatchEvent(new DataEvent(EquipmentModuleConst.Event_ChoosedJewelChange, [allItem[0].itemData], false, true));
                this.onButtonClickImpl(confirmButton);
            }
            return;
        }

        /* internal  */onConfirm(): void
        {
            var loc3=0;
            var loc5=null;
            var loc1=new Array<any>();
            var loc2=allItem[0].itemData;
            if (this.isEmbededPos() && this._embedEquip) 
            {
                loc1.push(this._embedEquip.uid);
            }
            else 
            {
                loc1.push(loc2.uid);
            }
            if (this._useItems && this._useItems.length > 0) 
            {
                var loc6=0;
                var loc7=this._useItems;
                for(loc5 of loc7) 
                {
                    loc1.push(loc5.uid);
                }
            }
            if (this.isEmbededPos()) 
            {
                loc3 = this.getTargetIndex(loc2.itemCode, this._embedEquip);
            }
            else 
            {
                loc3 = -1;
            }
            var loc4;
            (loc4 = new EquipUpdateParams()).operationId = this._operationId;
            loc4.autoUpdate = false;
            loc4.equipPosType = loc2.posType;
            loc4.materials = loc1;
            loc4.target = loc3;
            loc4.num = this._numInput.currentNum;
            loc4.flag = false;
            loc4.autoBuyTone = Cache.instance.equipment.isAutoBuy;
            this.dispatchEvent(new DataEvent(EquipmentModuleConst.Event_DoUpdateEvent1, loc4, false, true));
            return;
        }

        /* internal  */getTargetIndex(arg1: int, arg2: ItemData): int
        {
            var loc1=1;
            while (loc1 <= 6) 
            {
                if (arg2.itemExInfo["hole" + loc1] == arg1) 
                {
                    return loc1;
                }
                ++loc1;
            }
            return -1;
        }

        protected /* override */ updateStatus(): void
        {
            var loc1=0;
            if (allItem[0].itemData) 
            {
                loc1 = EquipAdvanceConfig.instance.getJewelAdvanceCost(allItem[0].itemData.itemInfo.item.itemLevel) * this._numInput.currentNum;
                this.showResultJewel();
            }
            else if (!allItem[0].itemData && !this._isResult) 
            {
                loc1 = 0;
                clearAllItem();
            }
            if (_cost != loc1) 
            {
                _cost = loc1;
                dispatchEvent(new DataEvent(EquipmentModuleConst.Event_CostChange, _cost, false, true));
            }
            return;
        }

        public /* override */ putInItem(arg1: ItemData, arg2: Boolean=false, arg3=null): void
        {
            if (arg1 && arg1.itemInfo.item.category == ECategory._ECategoryJewel) 
            {
                this._isResult = false;
                clearAllItem();
                this._embedEquip = arg3;
                allItem[0].itemData = arg1;
                allItem[0].amount = arg1.itemAmount;
                this._numInput.maxNum = arg1.itemAmount;
                this._numInput.currentNum = arg1.itemAmount;
                this.dispatchEvent(new DataEvent(EquipmentModuleConst.Event_ChoosedJewelChange, [allItem[0].itemData], false, true));
            }
            this.updateStatus();
            return;
        }

        public /* override */ setAutoChooseMaterial(arg1: Object): void
        {
            if (!allItem[0].itemData) 
            {
                return;
            }
            this._materialsOwn = arg1.material as Array<any>;
            this.updateMaterial();
            return;
        }

        /* internal  */updateMaterial(): void
        {
            var loc3=null;
            var loc5=null;
            this._useItems = [];
            var loc1=false;
            var loc2=0;
            var loc4=0;
            while (loc4 < this._materialsOwn.length) 
            {
                loc3 = this._materialsOwn[loc4] as ItemData;
                this._useItems.push(loc3);
                loc2 = loc2 + loc3.itemAmount;
                if (loc2 >= this._numInput.currentNum) 
                {
                    loc1 = true;
                    break;
                }
                ++loc4;
            }
            if (loc1 && this._useItems.length > 0) 
            {
                allItem[1].itemData = this._useItems[0] as ItemData;
            }
            else if (loc5 = ItemConfig.instance.getItemInfoByParams(ECategory._ECategoryProp, EProp._EPropJewelAdvance, EBind._EBindNo, -1, -1, allItem[0].itemData.itemInfo.item.itemLevel)) 
            {
                allItem[1].itemData = new ItemData(loc5.item.code);
            }
            allItem[1].amount = this._numInput.currentNum;
            allItem[1].enabled = loc1;
            this.updateStatus();
            return;
        }

        /* internal  */checkWillChangeBind(): Boolean
        {
            var loc1=0;
            if (allItem[0].itemData && !this.isEmbededPos() && !ItemsUtil.isBind(allItem[0].itemData)) 
            {
                loc1 = 0;
                while (loc1 < this._useItems.length) 
                {
                    if (this._useItems[loc1].itemInfo.item.bind == EBind._EBindYes) 
                    {
                        return true;
                    }
                    ++loc1;
                }
            }
            return false;
        }

        /* internal  */isEmbededPos(): Boolean
        {
            if (allItem[0].itemData) 
            {
                if (allItem[0].itemData.posType == EPlayerItemPosType._EPlayerItemPosTypeRole || allItem[0].itemData.posType >= EPlayerItemPosType._EPlayerItemPosTypePetEquipBase) 
                {
                    return true;
                }
            }
            return false;
        }

        /* internal  */showResultJewel(): void
        {
            if (!allItem[0].itemData) 
            {
                allItem[2].itemData = null;
                return;
            }
            var loc1=allItem[0].itemData;
            var loc2=ItemConfig.instance.getItemInfoByParams(ECategory._ECategoryJewel, loc1.type, loc1.bind, 2, -1, loc1.itemInfo.item.itemLevel);
            if (loc2) 
            {
                allItem[2].itemData = new ItemData(loc2.item.code);
                allItem[2].amount = this._numInput.currentNum;
            }
            else 
            {
                allItem[2].itemData = null;
            }
            return;
        }

        public moveOutByIndex(arg1: int): void
        {
            if (arg1 == 0) 
            {
                this.clear();
            }
            return;
        }

        public /* override */ clear(): void
        {
            this._isResult = false;
            this._useItems = [];
            this._embedEquip = null;
            this._numInput.maxNum = 1;
            clearAllItem();
            this.updateStatus();
            this.dispatchEvent(new DataEvent(EquipmentModuleConst.Event_ChoosedJewelChange, [], false, true));
            return;
        }

        public /* override */ onUpdateResult(arg1: int, arg2: Boolean, arg3, arg4: Array<any>): void
        {
            var loc1=null;
            if (arg1 == EItemUpdateResult._EItemUpdateResultSuccess) 
            {
                this.clear();
                this._isResult = true;
                loc1 = new ItemData(arg3);
                allItem[2].itemData = loc1;
                allItem[2].amount = loc1.itemAmount;
                MsgManager.showRollTipsMsg(Language.getString(61068));
                addUpdateSuccessEffect("equipEffect_qualityUp.swf", 175, 216);
            }
            return;
        }

        /* internal */ /* var */_operationId: int=19;

        /* internal */ /* var */_embedEquip: ItemData;

        /* internal */ /* var */_materialsOwn: Array<any>;

        /* internal */ /* var */_useItems: Array<any>;

        /* internal */ /* var */_isResult: Boolean=false;

        /* internal */ /* var */posData: Array<any>;

        /* internal */ /* var */_numInput: NumInput;

        /* internal */ /* var */_alarmText: string;

        /* internal */ /* var */buttonLabel: string;
    }
